<?php

namespace app\business\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class AfterSale extends Controller
{
    public function SaleCheck()
    {
        $tenantId=trim(input('param.tenantId'));
        $busId=trim(input('param.busId'));
        $expressNos=trim(input('param.expressNos'));
        $issue=trim(input('param.issue'));
        $img=trim(input('param.img'));
        $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
        if (strstr($img, ",")) {
            $image = explode(',', $img);
            $image = $image[1];
        }
        $path = "./uploads/" . date("Ymd", time());
        $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
        if (!is_dir($path)) { //判断目录是否存在 不存在就创建
            mkdir($path, 0777, true);
        }
        $imageSrc = $path . "/" . $imageName; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
        $imageSrcs = $paths . "/" . $imageName; //图片名字
        if (!$r) {
            exit(json_encode(['message' => 'error', 'result' => "失败"]));
           
        } else {
             $data['tenant_id']=$tenantId;
             $data['bus_id']= $busId;
             $data['express_nos']=$expressNos;
             $data['issue']=$issue;
             $data['img']=$imageSrcs;
             $this->db = new SysDb();
             $this->db->table('bus_after_sale')->insert($data);
            exit(json_encode(['message' => 'succ', 'result' =>$imageSrcs]));
          
        }

    }
    public function SaleChecklist()
    {
             $id = trim(input('param.id'));
             $page = trim(input('param.pageNo'));
             $limit= trim(input('param.pageSize'));
             $status=trim(input('param.status'));
            
             // $count = $this->db->table('newslist')->count();
             $tol = (intval($page) - 1) * $limit;
             // ->where('type', $type)
             if($status==10){
                $list =DB::name("bus_after_sale")->order("create_time desc")->where('bus_id', $id)->limit($tol, $limit)->select();
             }else{
              $list =DB::name("bus_after_sale")->order("create_time desc")->where('bus_id', $id)->where('status', $status)->limit($tol, $limit)->select();
            }
             // $list=$this->db->table('order_info')->where(['bus_id' =>$id])->lists();
             $this->db = new SysDb();
             $count = $this->db->table('bus_after_sale')->where(['bus_id' => $id]) ->count();
             $data=array();
             array_push($data,$list);
             array_push($data,$count);
             exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));

    }

    public function bottomSheet(){
        $busId=trim(input('param.id'));
        $expressNos=trim(input('param.expressNos'));
        $email=trim(input('param.email'));
        $tenantId=trim(input('param. tenantId'));
        $data['tenant_id']=$tenantId;
        $data['bus_id']= $busId;
        $data['express_nos']=$expressNos;
        $data['email']=$email;
        $data['status']=0;
        $this->db = new SysDb();
        $this->db->table('bus_bottom_sheet')->insert($data);
        exit(json_encode(['message' => 'succ', 'result' =>'添加成功']));
    }

    public function bottomSheetlist()
    {
             $id = trim(input('param.id'));
             $page = trim(input('param.pageNo'));
             $limit= trim(input('param.pageSize'));
             // $count = $this->db->table('newslist')->count();
             $tol = (intval($page) - 1) * $limit;
             // ->where('type', $type)
           
                $list =DB::name("bus_bottom_sheet")->order("create_time desc")->where('bus_id', $id)->limit($tol, $limit)->select();
            
             // $list=$this->db->table('order_info')->where(['bus_id' =>$id])->lists();
             $this->db = new SysDb();
             $count = $this->db->table('bus_bottom_sheet')->where(['bus_id' => $id]) ->count();
             $data=array();
             array_push($data,$list);
             array_push($data,$count);
             exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));

    }
}
